package org.vectrics.mail.struts;

import javax.servlet.jsp.JspException;

import org.vectrics.common.util.StringUtils;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.mail.FileEmailTemplateCategory;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

public class TemplateCategorySaveAction extends VectricsStrutsAction {

	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		EmailTemplateCategoryForm form = (EmailTemplateCategoryForm)actionData.getForm();
		
		if (actionData.wasButtonPressed("cancel")) {
			return "cancel";
		} else {
			FileEmailTemplateCategory category = null;
			if (StringUtils.isNullOrBlank(form.getId())) {
				category = new FileEmailTemplateCategory();
			} else {
				Long id = new Long(form.getId());
				category = CoreServiceLocator.getSystemService().findFileEmailTemplateCategory(id);
			}
			category.setCode(form.getCode());
			category.setDomain(ThreadProperties.getDomain());
			category.setDescription(form.getDescription());
			category.setVoided(false);
			
			ThreadProperties.getSession().saveOrUpdate(category);
			return "success";
		}
	}

}
